import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.logging.Logger;

//玩家
public class Player extends Unit{

    ArrayList<Coord> pathToTargetList;
    int index;
    Coord target;
    int step;
    Logger logger = MyLogger.getLogger();

    public Player(Team team, int x, int y, int hpLeft, int mp, int AttackRadiusX, int AttackRadiusY, int step) {
        this.x = x;
        this.y = y;
        this.hpLeft = hpLeft;
        this.hpTotal = hpLeft;
        this.mp = mp;
        this.damage = 30;
        this.attackRadius = 120;
        this.step = step;
        this.team = team;
    }

    @Override
    void move() {
        if (pathToTargetList != null)
        {
            if (index >= pathToTargetList.size() - 1)
            {
                pathToTargetList.clear();
                pathToTargetList = null;
                index = 0;
                return;

            }

            //logger.info(pathToTargetList.size() + " " + index);
            Coord coord = pathToTargetList.get(index++);
            this.x = coord.x;
            this.y = coord.y;
        }
    }

    void move(int x, int y)
    {
        if (this.x + x > 0 || this.x + x < GameFrame.backgroundBufferedImage.getWidth())
            this.x += x;

        if (this.y + y > 0 || this.y + y < GameFrame.backgroundBufferedImage.getHeight())
            this.y += y;
    }

    void updateTarget(Coord target)
    {
        this.target = target;
        pathToTargetList = GameFrame.map.pathFindInit(new Coord(x, y), target);
        index = 0;
        Collections.reverse(pathToTargetList);
    }

    @Override
    void paint(Graphics graphics) {
        //logger.info(x + ": " + y);
        int x = this.x;
        int y = this.y;

        //画玩家血条
        graphics.setColor(new Color(255 - hpLeft * 255 / hpTotal , hpLeft * 255 / hpTotal, 0));
        graphics.fillRect(x, y, hpLeft * 100 / hpTotal, 10);
        y += 10;
        graphics.setColor(new Color(0 , 255, 0));
        graphics.setColor(new Color(0, 255, 0));
        graphics.drawRect(x, y, 40, 10);

        y += 10;
        //画玩家蓝条
        graphics.setColor(new Color(0, 0, 255));
        graphics.drawRect(x, y, 40, 10);
        graphics.fillRect(x, y, 30, 10);


        //画玩家攻击半径
        graphics.setColor(new Color(255, 0, 0));
        graphics.drawArc(x - attackRadius / 2, y - attackRadius / 2, attackRadius, attackRadius * 2 / 3, 0, 360);


        //绘制路径
        if (pathToTargetList != null)
        {
            for(Coord coord: pathToTargetList)
            {
                graphics.fillRect(coord.x, coord.y, 3,3);
            }
        }

        y += 30;
        //画玩家，现在是画一个小球
        if (team == Team.RED)
        {
            graphics.setColor(new Color(255, 0, 0));
        }
        else
        {
            graphics.setColor(new Color(0, 0, 255));
        }

        graphics.drawString("英雄(" +x + "," + y + "), hpLeft: " + hpLeft,x,y);

        graphics.fillArc(x, y, 50,50,0,360);
    }


    @Override
    void attack(Graphics graphics) {
        if (state != UnitState.Attack)
            return;

        final Unit self = attack.self;
        final Unit target = attack.target;
        int incrementX = target.x - self.x;
        int incrementY = target.y - self.y;
        int ix = incrementX / 100;
        int iy = incrementY / 100;

        //1、画子弹
        int x = self.x + timer1 * ix;
        int y = self.y + timer1 * iy;

        //画子弹
        if (team == Team.RED)
        {
            graphics.setColor(new Color(255, 0,0));
        }
        else if (team == Team.BLUE)
        {
            graphics.setColor(new Color(0,0, 255));
        }

        graphics.fillRect(x, y, 5, 5);
        //logger.info(self.x + " " + self.y + " " + target.x + " " + target.y + " x,y: " + x + " " + y + " timer: " + timer1);

        timer1++;

        //2、对方减血量
        if (Math.abs(x - target.x) < 10 || Math.abs(y - target.y) < 10)
        {
            timer1 = 0;
            target.hpLeft -= (int) (damage + Math.random() * 5); //攻击力是5-10
        }
    }

}
